<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - svm_c_ex.cpp</title></head><body bgcolor='white'><pre>
<font color='#009900'>// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
</font><font color='#009900'>/*

    This is an example illustrating the use of the support vector machine
    utilities from the dlib C++ Library.  In particular, we show how to use the
    C parametrization of the SVM in this example.

    This example creates a simple set of data to train on and then shows
    you how to use the cross validation and svm training functions
    to find a good decision function that can classify examples in our
    data set.


    The data used in this example will be 2 dimensional data and will
    come from a distribution where points with a distance less than 10
    from the origin are labeled +1 and all other points are labeled
    as -1.
        
*/</font>


<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>iostream<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>dlib<font color='#5555FF'>/</font>svm.h<font color='#5555FF'>&gt;</font>

<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std;
<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> dlib;


<font color='#0000FF'><u>int</u></font> <b><a name='main'></a>main</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
<b>{</b>
    <font color='#009900'>// The svm functions use column vectors to contain a lot of the data on
</font>    <font color='#009900'>// which they operate. So the first thing we do here is declare a convenient
</font>    <font color='#009900'>// typedef.  
</font>
    <font color='#009900'>// This typedef declares a matrix with 2 rows and 1 column.  It will be the
</font>    <font color='#009900'>// object that contains each of our 2 dimensional samples.   (Note that if
</font>    <font color='#009900'>// you wanted more than 2 features in this vector you can simply change the
</font>    <font color='#009900'>// 2 to something else.  Or if you don't know how many features you want
</font>    <font color='#009900'>// until runtime then you can put a 0 here and use the matrix.set_size()
</font>    <font color='#009900'>// member function)
</font>    <font color='#0000FF'>typedef</font> matrix<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font>, <font color='#979000'>2</font>, <font color='#979000'>1</font><font color='#5555FF'>&gt;</font> sample_type;

    <font color='#009900'>// This is a typedef for the type of kernel we are going to use in this
</font>    <font color='#009900'>// example.  In this case I have selected the radial basis kernel that can
</font>    <font color='#009900'>// operate on our 2D sample_type objects.  You can use your own custom
</font>    <font color='#009900'>// kernels with these tools as well, see <a href="custom_trainer_ex.cpp.html">custom_trainer_ex.cpp</a> for an
</font>    <font color='#009900'>// example.
</font>    <font color='#0000FF'>typedef</font> radial_basis_kernel<font color='#5555FF'>&lt;</font>sample_type<font color='#5555FF'>&gt;</font> kernel_type;


    <font color='#009900'>// Now we make objects to contain our samples and their respective labels.
</font>    std::vector<font color='#5555FF'>&lt;</font>sample_type<font color='#5555FF'>&gt;</font> samples;
    std::vector<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font><font color='#5555FF'>&gt;</font> labels;

    <font color='#009900'>// Now let's put some data into our samples and labels objects.  We do this
</font>    <font color='#009900'>// by looping over a bunch of points and labeling them according to their
</font>    <font color='#009900'>// distance from the origin.
</font>    <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>int</u></font> r <font color='#5555FF'>=</font> <font color='#5555FF'>-</font><font color='#979000'>20</font>; r <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>20</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>r<font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>int</u></font> c <font color='#5555FF'>=</font> <font color='#5555FF'>-</font><font color='#979000'>20</font>; c <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>20</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>c<font face='Lucida Console'>)</font>
        <b>{</b>
            sample_type samp;
            <font color='#BB00BB'>samp</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> r;
            <font color='#BB00BB'>samp</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> c;
            samples.<font color='#BB00BB'>push_back</font><font face='Lucida Console'>(</font>samp<font face='Lucida Console'>)</font>;

            <font color='#009900'>// if this point is less than 10 from the origin
</font>            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>sqrt</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font><font face='Lucida Console'>)</font>r<font color='#5555FF'>*</font>r <font color='#5555FF'>+</font> c<font color='#5555FF'>*</font>c<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>10</font><font face='Lucida Console'>)</font>
                labels.<font color='#BB00BB'>push_back</font><font face='Lucida Console'>(</font><font color='#5555FF'>+</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>else</font>
                labels.<font color='#BB00BB'>push_back</font><font face='Lucida Console'>(</font><font color='#5555FF'>-</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>;

        <b>}</b>
    <b>}</b>


    <font color='#009900'>// Here we normalize all the samples by subtracting their mean and dividing
</font>    <font color='#009900'>// by their standard deviation.  This is generally a good idea since it
</font>    <font color='#009900'>// often heads off numerical stability problems and also prevents one large
</font>    <font color='#009900'>// feature from smothering others.  Doing this doesn't matter much in this
</font>    <font color='#009900'>// example so I'm just doing this here so you can see an easy way to
</font>    <font color='#009900'>// accomplish it.  
</font>    vector_normalizer<font color='#5555FF'>&lt;</font>sample_type<font color='#5555FF'>&gt;</font> normalizer;
    <font color='#009900'>// Let the normalizer learn the mean and standard deviation of the samples.
</font>    normalizer.<font color='#BB00BB'>train</font><font face='Lucida Console'>(</font>samples<font face='Lucida Console'>)</font>;
    <font color='#009900'>// now normalize each sample
</font>    <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> samples.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
        samples[i] <font color='#5555FF'>=</font> <font color='#BB00BB'>normalizer</font><font face='Lucida Console'>(</font>samples[i]<font face='Lucida Console'>)</font>; 


    <font color='#009900'>// Now that we have some data we want to train on it.  However, there are
</font>    <font color='#009900'>// two parameters to the training.  These are the C and gamma parameters.
</font>    <font color='#009900'>// Our choice for these parameters will influence how good the resulting
</font>    <font color='#009900'>// decision function is.  To test how good a particular choice of these
</font>    <font color='#009900'>// parameters are we can use the cross_validate_trainer() function to perform
</font>    <font color='#009900'>// n-fold cross validation on our training data.  However, there is a
</font>    <font color='#009900'>// problem with the way we have sampled our distribution above.  The problem
</font>    <font color='#009900'>// is that there is a definite ordering to the samples.  That is, the first
</font>    <font color='#009900'>// half of the samples look like they are from a different distribution than
</font>    <font color='#009900'>// the second half.  This would screw up the cross validation process but we
</font>    <font color='#009900'>// can fix it by randomizing the order of the samples with the following
</font>    <font color='#009900'>// function call.
</font>    <font color='#BB00BB'>randomize_samples</font><font face='Lucida Console'>(</font>samples, labels<font face='Lucida Console'>)</font>;


    <font color='#009900'>// here we make an instance of the svm_c_trainer object that uses our kernel
</font>    <font color='#009900'>// type.
</font>    svm_c_trainer<font color='#5555FF'>&lt;</font>kernel_type<font color='#5555FF'>&gt;</font> trainer;

    <font color='#009900'>// Now we loop over some different C and gamma values to see how good they
</font>    <font color='#009900'>// are.  Note that this is a very simple way to try out a few possible
</font>    <font color='#009900'>// parameter choices.  You should look at the <a href="model_selection_ex.cpp.html">model_selection_ex.cpp</a> program
</font>    <font color='#009900'>// for examples of more sophisticated strategies for determining good
</font>    <font color='#009900'>// parameter choices.
</font>    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>doing cross validation</font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
    <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font> gamma <font color='#5555FF'>=</font> <font color='#979000'>0.00001</font>; gamma <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>1</font>; gamma <font color='#5555FF'>*</font><font color='#5555FF'>=</font> <font color='#979000'>5</font><font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font> C <font color='#5555FF'>=</font> <font color='#979000'>1</font>; C <font color='#5555FF'>&lt;</font> <font color='#979000'>100000</font>; C <font color='#5555FF'>*</font><font color='#5555FF'>=</font> <font color='#979000'>5</font><font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#009900'>// tell the trainer the parameters we want to use
</font>            trainer.<font color='#BB00BB'>set_kernel</font><font face='Lucida Console'>(</font><font color='#BB00BB'>kernel_type</font><font face='Lucida Console'>(</font>gamma<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            trainer.<font color='#BB00BB'>set_c</font><font face='Lucida Console'>(</font>C<font face='Lucida Console'>)</font>;

            cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>gamma: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> gamma <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>    C: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> C;
            <font color='#009900'>// Print out the cross validation accuracy for 3-fold cross validation using
</font>            <font color='#009900'>// the current gamma and C.  cross_validate_trainer() returns a row vector.
</font>            <font color='#009900'>// The first element of the vector is the fraction of +1 training examples
</font>            <font color='#009900'>// correctly classified and the second number is the fraction of -1 training
</font>            <font color='#009900'>// examples correctly classified.
</font>            cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>     cross validation accuracy: </font>" 
                 <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>cross_validate_trainer</font><font face='Lucida Console'>(</font>trainer, samples, labels, <font color='#979000'>3</font><font face='Lucida Console'>)</font>;
        <b>}</b>
    <b>}</b>


    <font color='#009900'>// From looking at the output of the above loop it turns out that good
</font>    <font color='#009900'>// values for C and gamma for this problem are 5 and 0.15625 respectively.
</font>    <font color='#009900'>// So that is what we will use.
</font>
    <font color='#009900'>// Now we train on the full set of data and obtain the resulting decision
</font>    <font color='#009900'>// function.  The decision function will return values &gt;= 0 for samples it
</font>    <font color='#009900'>// predicts are in the +1 class and numbers &lt; 0 for samples it predicts to
</font>    <font color='#009900'>// be in the -1 class.
</font>    trainer.<font color='#BB00BB'>set_kernel</font><font face='Lucida Console'>(</font><font color='#BB00BB'>kernel_type</font><font face='Lucida Console'>(</font><font color='#979000'>0.15625</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
    trainer.<font color='#BB00BB'>set_c</font><font face='Lucida Console'>(</font><font color='#979000'>5</font><font face='Lucida Console'>)</font>;
    <font color='#0000FF'>typedef</font> decision_function<font color='#5555FF'>&lt;</font>kernel_type<font color='#5555FF'>&gt;</font> dec_funct_type;
    <font color='#0000FF'>typedef</font> normalized_function<font color='#5555FF'>&lt;</font>dec_funct_type<font color='#5555FF'>&gt;</font> funct_type;

    <font color='#009900'>// Here we are making an instance of the normalized_function object.  This
</font>    <font color='#009900'>// object provides a convenient way to store the vector normalization
</font>    <font color='#009900'>// information along with the decision function we are going to learn.  
</font>    funct_type learned_function;
    learned_function.normalizer <font color='#5555FF'>=</font> normalizer;  <font color='#009900'>// save normalization information
</font>    learned_function.function <font color='#5555FF'>=</font> trainer.<font color='#BB00BB'>train</font><font face='Lucida Console'>(</font>samples, labels<font face='Lucida Console'>)</font>; <font color='#009900'>// perform the actual SVM training and save the results
</font>
    <font color='#009900'>// print out the number of support vectors in the resulting decision function
</font>    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\nnumber of support vectors in our learned_function is </font>" 
         <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> learned_function.function.basis_vectors.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#009900'>// Now let's try this decision_function on some samples we haven't seen before.
</font>    sample_type sample;

    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>3.123</font>;
    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>2</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>This is a +1 class example, the classifier output is </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>learned_function</font><font face='Lucida Console'>(</font>sample<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>3.123</font>;
    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>9.3545</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>This is a +1 class example, the classifier output is </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>learned_function</font><font face='Lucida Console'>(</font>sample<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>13.123</font>;
    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>9.3545</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>This is a -1 class example, the classifier output is </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>learned_function</font><font face='Lucida Console'>(</font>sample<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>13.123</font>;
    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>This is a -1 class example, the classifier output is </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>learned_function</font><font face='Lucida Console'>(</font>sample<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;


    <font color='#009900'>// We can also train a decision function that reports a well conditioned
</font>    <font color='#009900'>// probability instead of just a number &gt; 0 for the +1 class and &lt; 0 for the
</font>    <font color='#009900'>// -1 class.  An example of doing that follows:
</font>    <font color='#0000FF'>typedef</font> probabilistic_decision_function<font color='#5555FF'>&lt;</font>kernel_type<font color='#5555FF'>&gt;</font> probabilistic_funct_type;  
    <font color='#0000FF'>typedef</font> normalized_function<font color='#5555FF'>&lt;</font>probabilistic_funct_type<font color='#5555FF'>&gt;</font> pfunct_type;

    pfunct_type learned_pfunct; 
    learned_pfunct.normalizer <font color='#5555FF'>=</font> normalizer;
    learned_pfunct.function <font color='#5555FF'>=</font> <font color='#BB00BB'>train_probabilistic_decision_function</font><font face='Lucida Console'>(</font>trainer, samples, labels, <font color='#979000'>3</font><font face='Lucida Console'>)</font>;
    <font color='#009900'>// Now we have a function that returns the probability that a given sample is of the +1 class.  
</font>
    <font color='#009900'>// print out the number of support vectors in the resulting decision function.  
</font>    <font color='#009900'>// (it should be the same as in the one above)
</font>    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\nnumber of support vectors in our learned_pfunct is </font>" 
         <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> learned_pfunct.function.decision_funct.basis_vectors.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>3.123</font>;
    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>2</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>This +1 class example should have high probability.  Its probability is: </font>" 
         <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>learned_pfunct</font><font face='Lucida Console'>(</font>sample<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>3.123</font>;
    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>9.3545</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>This +1 class example should have high probability.  Its probability is: </font>" 
         <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>learned_pfunct</font><font face='Lucida Console'>(</font>sample<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>13.123</font>;
    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>9.3545</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>This -1 class example should have low probability.  Its probability is: </font>" 
         <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>learned_pfunct</font><font face='Lucida Console'>(</font>sample<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>13.123</font>;
    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>This -1 class example should have low probability.  Its probability is: </font>" 
         <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>learned_pfunct</font><font face='Lucida Console'>(</font>sample<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;



    <font color='#009900'>// Another thing that is worth knowing is that just about everything in dlib
</font>    <font color='#009900'>// is serializable.  So for example, you can save the learned_pfunct object
</font>    <font color='#009900'>// to disk and recall it later like so:
</font>    <font color='#BB00BB'>serialize</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>saved_function.dat</font>"<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> learned_pfunct;

    <font color='#009900'>// Now let's open that file back up and load the function object it contains.
</font>    <font color='#BB00BB'>deserialize</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>saved_function.dat</font>"<font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font> learned_pfunct;

    <font color='#009900'>// Note that there is also an example program that comes with dlib called
</font>    <font color='#009900'>// the <a href="file_to_code_ex.cpp.html">file_to_code_ex.cpp</a> example.  It is a simple program that takes a
</font>    <font color='#009900'>// file and outputs a piece of C++ code that is able to fully reproduce the
</font>    <font color='#009900'>// file's contents in the form of a std::string object.  So you can use that
</font>    <font color='#009900'>// along with the std::istringstream to save learned decision functions
</font>    <font color='#009900'>// inside your actual C++ code files if you want.  
</font>



    <font color='#009900'>// Lastly, note that the decision functions we trained above involved well
</font>    <font color='#009900'>// over 200 basis vectors.  Support vector machines in general tend to find
</font>    <font color='#009900'>// decision functions that involve a lot of basis vectors.  This is
</font>    <font color='#009900'>// significant because the more basis vectors in a decision function, the
</font>    <font color='#009900'>// longer it takes to classify new examples.  So dlib provides the ability
</font>    <font color='#009900'>// to find an approximation to the normal output of a trainer using fewer
</font>    <font color='#009900'>// basis vectors.  
</font>
    <font color='#009900'>// Here we determine the cross validation accuracy when we approximate the
</font>    <font color='#009900'>// output using only 10 basis vectors.  To do this we use the reduced2()
</font>    <font color='#009900'>// function.  It takes a trainer object and the number of basis vectors to
</font>    <font color='#009900'>// use and returns a new trainer object that applies the necessary post
</font>    <font color='#009900'>// processing during the creation of decision function objects.
</font>    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\ncross validation accuracy with only 10 support vectors: </font>" 
         <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>cross_validate_trainer</font><font face='Lucida Console'>(</font><font color='#BB00BB'>reduced2</font><font face='Lucida Console'>(</font>trainer,<font color='#979000'>10</font><font face='Lucida Console'>)</font>, samples, labels, <font color='#979000'>3</font><font face='Lucida Console'>)</font>;

    <font color='#009900'>// Let's print out the original cross validation score too for comparison.
</font>    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>cross validation accuracy with all the original support vectors: </font>" 
         <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>cross_validate_trainer</font><font face='Lucida Console'>(</font>trainer, samples, labels, <font color='#979000'>3</font><font face='Lucida Console'>)</font>;

    <font color='#009900'>// When you run this program you should see that, for this problem, you can
</font>    <font color='#009900'>// reduce the number of basis vectors down to 10 without hurting the cross
</font>    <font color='#009900'>// validation accuracy. 
</font>

    <font color='#009900'>// To get the reduced decision function out we would just do this:
</font>    learned_function.function <font color='#5555FF'>=</font> <font color='#BB00BB'>reduced2</font><font face='Lucida Console'>(</font>trainer,<font color='#979000'>10</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>train</font><font face='Lucida Console'>(</font>samples, labels<font face='Lucida Console'>)</font>;
    <font color='#009900'>// And similarly for the probabilistic_decision_function: 
</font>    learned_pfunct.function <font color='#5555FF'>=</font> <font color='#BB00BB'>train_probabilistic_decision_function</font><font face='Lucida Console'>(</font><font color='#BB00BB'>reduced2</font><font face='Lucida Console'>(</font>trainer,<font color='#979000'>10</font><font face='Lucida Console'>)</font>, samples, labels, <font color='#979000'>3</font><font face='Lucida Console'>)</font>;
<b>}</b>


</pre></body></html>